//
//  YPAutoTenderViewController.m
//  ChengHuiTong
//
//  Created by xzm on 16/8/5.
//  Copyright © 2016年 张文权. All rights reserved.
//

#import "YPAutoTenderViewController.h"
#import "YPAutoTenderCell.h"
#import "YPAutoTenderModel.h"
#import "UIView+addLine.h"
#import "YPATSelectViewController.h"
#import "YPAtuoDetailViewController.h"
@interface YPAutoTenderViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    NSArray *_titleArr;
    NSArray *_remindArr;
    NSMutableArray *_selectArr;
    NSDictionary *_dataDic;
    
}

@property (nonatomic,strong)UITableView *atTableView;

@end

@implementation YPAutoTenderViewController

#pragma mark -- 懒加载
- (UITableView *)atTableView{
    
    if (!_atTableView) {
        
        _atTableView = [[UITableView alloc]initWithFrame:CGRectMake(12, 0, SCREEN_WIDTH - 24, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
        _atTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _atTableView.backgroundColor = UIColorFromRGB(0xf5f5f5);
        _atTableView.showsVerticalScrollIndicator = NO;
        _atTableView.delegate = self;
        _atTableView.dataSource = self;
    }
    
    return _atTableView;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setUpData];
    
    self.title = @"自动投标";

}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self getData];
}

-(void)getData{
    
    YPautoListBin*bin=[[YPautoListBin alloc] init];
    
    bin.token=[YPShareAccountInfo shareAccountInfo].token;

    bin.trust=[NSString stringWithFormat:@"%@",[NSNumber numberWithBool:self.trust]];
    
    [YPHttpRequestMethod sendAsyRequestURL:autoList withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
        
        [_selectArr removeAllObjects];

        if ([responseDictionary[@"status"] intValue]==0) {
            
            [self setUpUI];
            
            _dataDic = responseDictionary;
            
            if ([_dataDic[@"hs_flag"] integerValue] == 1) {
                
                if (self.trust && [_dataDic[@"is_sign"] intValue]==0) {
                    WeakSelf
                    [self showWarnViewWithText:@"设置自动投标前，您需前往徽商银行签约！" textAlignment:1 buttonTitle:@"确定" block:^{
                        //自动投标 - 丰付支付签约
                        YPGoWebView(String_Connect(CMD_AUTOBIDSIGN, @"&token", TOKEN), weakSelf.navigationController);
                    }];
                }
                
            }else{
                
                [self showHsBoxWithFlag:[_dataDic[@"hs_flag"] integerValue]];
            }

            NSArray*list=_dataDic[@"list"];
            
            for (NSDictionary*dic in list) {
                
                autoListModel*listModel=[[autoListModel alloc] initWithDic:dic];
                
                [_selectArr addObject:listModel];
                
            }
            
            [self.atTableView reloadData];
            
        }
        
    } withFailedBlock:^(NSError *error) {
        
    } withIndicator:self.view.subviews.count > 0? NO:YES atView:self.view];
    
}

#pragma mark -- 初始化
- (void)setUpUI{

    if(![self.view.subviews containsObject:self.atTableView]) [self.view addSubview:self.atTableView];
    
}

- (void)setUpData{
    
    _titleArr = @[@"投标金额",@"借款期限",@"有效时间",@"更多"];
    _remindArr = @[@"1.用户开启此工具，并完成条件设置后，自动投标功能将在五分钟内生效；",@"2.自动投标成交的金额为用户在“自动投标”设置的最大金额和项目可投金额两者中的最小值；",@"3.用户开通自动投标功能即视为委托诚汇通平台与达到用户指定标准的借款人签署借款合同，并承担该合同项下的一切权利及义务；",@"4.在生效期内，用户的自动投标功能不会自动关闭，如果需要关闭，需进行手工设置；",@"5.用户依开通自动投标功能时间先后形成队列，平台项目在此队列依次进行匹配。若用户账户余额小于其设置的最小金额，在自动投标运行到该用户时，该用户将会被自动排到队尾；若项目不符合用户设置的规则，则用户保持在原有队列中位置；",@"6.用户任何对于自动投标规则的调整和开关都会导致用户重新排队到队尾，请编辑规则时保持谨慎。"];
    _selectArr = [NSMutableArray array];
}

#pragma mark -- 事件响应
- (void)addTenderAction{
    
    if ([_dataDic[@"hs_flag"] integerValue] != 1 || (self.trust && [_dataDic[@"is_sign"] intValue]==0)) {
        return;
    }
    
    YPATSelectViewController *selectVC= [[YPATSelectViewController alloc]init];
        
    selectVC.isTrust=self.trust;
        
    [self.navigationController pushViewController:selectVC animated:YES];
    
}

#pragma mark -- TableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return _selectArr.count;
    }
    return _remindArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        static NSString *identifier1 = @"cell1";
        YPAutoTenderCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier1];
        if (!cell) {
            
            cell = [[YPAutoTenderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier1];
            
            
        }
        cell.model=_selectArr[indexPath.row];
        
        cell.goDetail=^(NSString *autoid){
            
            
            YPAtuoDetailViewController*VC=[[YPAtuoDetailViewController alloc] init];
            
            VC.Bid=autoid;
            
            [self.navigationController pushViewController:VC animated:YES];
        };
        return cell;
        
    }else{
        static NSString *identifier2 = @"cell2";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier2];
        if (!cell) {
            
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier2];
            cell.backgroundColor = self.view.backgroundColor;
            cell.selectionStyle = 0;
            cell.textLabel.font = [UIFont systemFontOfSize:13];
            cell.textLabel.textColor = UIColorFromString(@"#666666");
            cell.textLabel.numberOfLines = 0;
            //[cell.contentView addSubview:label];
        }
        
        cell.textLabel.text=_remindArr[indexPath.row];
        
        return cell;
    }
}

#pragma mark -- TableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        return 66;
    }
    NSString *str = _remindArr[indexPath.row];
    return [str sizeWithFont:[UIFont systemFontOfSize:13] andMaxSize:CGSizeMake(self.atTableView.width - autoScaleW(30), CGFLOAT_MAX)].height + 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    if (section == 0) {
        
        CGFloat width = self.atTableView.width;
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.atTableView.width, 40)];
        
        
        
        
        
        NSArray*leftArr=@[[NSString stringWithFormat:@"0"],
                          [NSString stringWithFormat:@"%f",width*0.3],
                          [NSString stringWithFormat:@"%f",width*0.3+width*0.2],
                          [NSString stringWithFormat:@"%f",width*0.3+width*0.2+width*0.3]];
        
        NSArray*withArr=@[[NSString stringWithFormat:@"%f",width*0.3],
                          [NSString stringWithFormat:@"%f",width*0.2],
                          [NSString stringWithFormat:@"%f",width*0.3],
                          [NSString stringWithFormat:@"%f",width*0.2]];
        
        
        for (int i = 0; i < _titleArr.count; i ++) {
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake([leftArr[i] floatValue], 10, [withArr[i] floatValue], view.height - 10)];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:12];
            label.textColor = UIColorFromString(@"#FFFFFF");
            label.backgroundColor = kColor_AppNavColor;
            label.text = _titleArr[i];
            [view addSubview:label];
            if (i < _titleArr.count - 1) {
                
                [view addLineWithFrame:CGRectMake(label.right-0.5, label.top, 0.5, label.height) color:ColorWithRGB(220, 220, 220)];
            }
        }
        return view;
    }else{
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, 100, 40)];
        label.text = @"温馨提示:";
        label.font = [UIFont boldSystemFontOfSize:17];
        return label;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section == 0) {
        
        if (_selectArr.count == 0) {
            
            return 70;
        }
        return 35;
    }
    return 0.1;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (section == 0) {
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.atTableView.width, _selectArr.count == 0?70:35)];
        if (_selectArr.count == 0) {
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, view.width, 35)];
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = UIColorFromString(@"#7C7C7C");
            label.font = [UIFont systemFontOfSize:12];
            label.text = @"暂无自动投标记录";
            [view addSubview:label];
        }
        
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, view.width, 35)];
        btn.backgroundColor = kColor_AppNavColor;
        [btn addTarget:self action:@selector(addTenderAction) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:@"添加自动投标" forState:0];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        btn.bottom = view.height;
        [view addSubview:btn];
        return view;
    }
    return nil;
}

@end
